<?php

class CategoryComponents {
	
	public static function getCategoryList() {
		$html = '';
		$categories = db::getInstance()->getObjects('oeCategory', '`categoryId` IS NULL AND `status`=:visible', array('visible' => oeCategory::CATEGORY_STATUS_VISIBLE));
		
		$html .= '<h3>Categories</h3>';
		$html .='<ul>';
		foreach ($categories as $key => $category) {
			$html .= '<li><a href="/category/edit?id='. $category->getId() .'">'. $category->getName() .'</a> - <a href="/category/delete?id='. $category->getId() .'">delete</a></li>';
			$subCategories = $category->getSubCategories();
			if (!empty($subCategories)) {
				$html .= '<ul>';	
				foreach ($subCategories as $subCat) {
					$html .= '<li><a href="/category/edit?id='. $subCat->getId() .'">'. $subCat->getName() .'</a> - <a href="/category/delete?id='. $subCat->getId() .'">delete</a></li>';
				}
				$html .= '</ul>';
			}	
		}
		$html .= '</ul>';
		$html .= '<a href="/category/edit">add category</a> | <a href="/">back</a>';
		
		return $html;	
			
	}
	
	public static function getCategoryEdit($params) {
		$idInput = '';
		if (isset($params['id']) && $params['id'] > 0) {
			$obj = new oeCategory($params['id']);
			if ($obj->getId() > 0) {
				$idInput .= '<input type="hidden" name="id" value="'. $obj->getId() .'"/>';
				$txtName = $obj->getName();
				$subCategoryId = $obj->getCategoryId();	
			}
		}
		$txtName = isset($params['name']) ? $params['name'] : isset($txtName) ? $txtName : '';
		
		$categories = db::getInstance()->getObjects('oeCategory', '`categoryId` IS NULL AND `status`=:visible', array('visible' => oeCategory::CATEGORY_STATUS_VISIBLE));
		$cobCategoryOption = '<option value="0">Select a parent category (if neccessary)</option>';
		if (!empty($categories)) {
			foreach ($categories as $category) {
				if (isset($obj) && $category->getId() == $obj->getId())
					continue;
				$default = '';	
				if (isset($subCategoryId) && $category->getId() == $subCategoryId)
					$default .= 'selected="selected"';
				$cobCategoryOption .= '<option value="'. $category->getId() .'" '.$default.'>'. $category->getName() .'</option>';
			}	
		}
		
		$error = '';
		if (isset($params['errors'])) {
			foreach ($params['errors'] as $err) {
				$error .= $err.'<br/>';
			}
		}
		$success = '';
		if (isset($_GET['save']))
			$success = 'category changed';
			
		return <<<CONTENT
			<form action="/category/edit" method="post">
				<p class="text-error">{$error}</p>
				<p class="text-success">{$success}</p>
				<label for="txtName">name</label>
				<input type="text" id="txtName" name="txtName" value="{$txtName}" class="form-control" placeholder="enter category name" />
				<label for="cobCategoryId">Parent Category</label>
				<select id="cobCategoryId" name="cobCategoryId" class="form-control">
					{$cobCategoryOption}
				</select>
				{$idInput}
				<input type="hidden" name="submit" value="1"/>
				<input type="submit"/> <a href="/category">back</a>
			</form>
CONTENT;
	}

	public static function getDeleteForm($params) {
		
		if (isset($params['id'])) {
			$obj = new oeCategory($params['id']);
			if ($obj->getId() > 0 && $obj->isVisible()) {
				return <<<CONTENT
					<form action="/category/delete" method="post">
						<p class="foprm-helper">Are you sure you wish to delete this category?
							<br/>
							Category Name: {$obj->getName()}
						</p>
						<input type="hidden" name="id" value="{$obj->getId()}"/>
						<input type="hidden" name="submit" value="1"/>
						<input type="submit" value="yes"/>
					</form>
CONTENT;
			}
			else {
				$params['errors'][] = 'Category has already been deleted';
			}
		}
		else {
			$params['errors'][] = 'Unknown Category';
		}
		return CategoryComponents::getErrorCategory($params);
	}
	
	public static function getErrorCategory($params) {
		
		$error = '';		
		foreach ($params['errors'] as $err) {
			$error .= '<p class="text-error">'. $err .'</p>';
		}
		return <<<CONTENT
			<p>An error has occured:</p>
			{$error}
			<a href="/category">back</a>
CONTENT;
	}
}